<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="180"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  queryTrackingTask,
  queryTrackingTaskByid,
  addTrackingTask,
  modifyTrackingTask
} from '@/api/potenCus/basedata/followTask';
import { getDataType } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'SeriesModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      dataType: getDataType('dataType'),
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log('this.rowData',this.rowData)
      console.log('dataType', this.dataType)
      this.taskType=70251001
      if( this.dataType !=10461001 ){
        this.formList.find(item => item.fieldName ==='shopIntervalDays').hidden = true
        this.formList.find(item => item.fieldName ==='intervalDays').label = '间隔天数'
      }
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.taskId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        if( this.rowData.intentLevel ==15191005 ){
          this.formList.find(item => item.fieldName ==='isValid').disabled = true
        }
        if( this.dataType ==10461001 ){
          this.formList.find(item => item.fieldName ==='intentLevel').disabled = true
          this.formList.find(item => item.fieldName ==='taskId').disabled = true
          this.formList.find(item => item.fieldName ==='intervalDays').disabled = true
          this.formList.find(item => item.fieldName ==='taskName').disabled = true
          this.formList.find(item => item.fieldName ==='isValid').disabled = true
        }
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '意向级别',
          fieldName: 'intentLevel',
          placeholder: '请选择',
          itemList: this.createDictList('1519'),
          disabled: false,
          change: val => {
            this.changeIntentLevel(val);
          },
          rules: [{ required: true, message: '意向级别不能为空', trigger: 'blur' }]
        },{ // 与上一条是一个来源，展示文字不同
          type: 'SELECT',
          label: '所属意向类型',
          fieldName: 'intentLevel',
          placeholder: '请选择',
          disabled: true,
          itemList: this.preDictHandle('1519'),
          rules: [{ required: true, message: '意向级别不能为空', trigger: 'blur' }]
        },
        { // 本条数据隐藏，用上一条展示代替
          type: 'SELECT',
          label: '所属意向类型',
          fieldName: 'taskId',
          placeholder: '',
          itemList: [],
          initialValue: '',
          disabled: true,
          hidden:true,
          rules: [{ required: true, message: '所属意向类型不能为空', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '间隔天数(须小于等于默认值)',
          fieldName: 'shopIntervalDays',
          placeholder: '间隔天数',
          min: 0,
          initialValue: 0,
          hidden: false,
          rules: [{ required: true, message: '间隔天数不能为空', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '间隔天数(默认)',
          fieldName: 'intervalDays',
          placeholder: '间隔天数',
          initialValue: 0,
          disabled: false,
          rules: [{ required: true, message: '间隔天数不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '意向类型名称',
          maxlength: 30,
          fieldName: 'taskName',
          placeholder: '意向类型名称',
          disabled: false,
          rules: [{ required: true, message: '意向类型名称不能为空', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          disabled: false,
          // disabled: this.formList.find(item => item.fieldName ==='intentLevel').initialValue ==15191005 ? true : false,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    changeIntentLevel(val) {
      console.log('val',val)
      this.formList[1].initialValue = val
      let data = JSON.parse(localStorage.getItem('dict'))
      console.log('data',this.formList)
      let  res = data[1519].map(x => ({ text: x.codeEnDesc, value: x.codeId }));
      console.log(res,"789")
      this.formList[1].itemList=res
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log("保存", this.saveData)
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addTrackingTask({...this.saveData,taskType:this.taskType});
        }
        if (this.type === 'edit') {
          if(this.saveData.shopIntervalDays > this.saveData.intervalDays){
            this.$message.info('间隔天数须小于等于默认值')
          } else{
            res = await modifyTrackingTask({...this.saveData,taskType:this.taskType,taskId:this.recordId},this.recordId);
          }
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    preDictHandle (code) {
      let resultList = this.dict[code].map(item => {
        return { value: item.codeId, text: item.codeEnDesc}
      })
      return resultList
    }
  }
};
</script>

<style lang="less" scoped>
</style>
